package com.lilong.jwt.service.user.impl;

import com.google.common.collect.Lists;
import com.lilong.jwt.common.base.BaseRepository;
import com.lilong.jwt.common.base.BaseServiceImpl;
import com.lilong.jwt.common.exception.ExceptionEnum;
import com.lilong.jwt.common.exception.HydrauException;
import com.lilong.jwt.common.util.PasswordUtil;
import com.lilong.jwt.entity.user.QUser;
import com.lilong.jwt.entity.user.User;
import com.lilong.jwt.repository.user.UserRepository;
import com.lilong.jwt.service.user.UserService;
import com.querydsl.core.types.dsl.EntityPathBase;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    @Resource
    private UserRepository userRepository;

    @Override
    protected String getEntityMsg() {
        return "用户信息";
    }

    @Override
    protected BaseRepository<User> getBaseRepository() {
        return userRepository;
    }

    @Override
    protected EntityPathBase<User> getQEntity() {
        return QUser.user;
    }

    protected void check(User t) {
        User user = userRepository.findByUsername(t.getUsername());
        if (!ObjectUtils.isEmpty(user)) {
            throw new HydrauException(ExceptionEnum.ALREADY_EXIST, getEntityMsg());
        }
        super.check(t);
    }

    public User saveEntity(User t, User operator) {
        t.setPassword(PasswordUtil.getPassword(t.getPassword()));
        t.setStatus(UserStatusEnum.ENABLE.getValue());
        return super.saveEntity(t, operator);
    }

    @Transactional(rollbackFor = Exception.class)
    public User findById(Long id) {
        return super.findById(id);
    }

    @Override
    public boolean able(Long id, User operator) {
        User user = super.findById(id);
        if (ObjectUtils.isEmpty(user)) {
            throw new HydrauException(ExceptionEnum.NOT_FOUND, getEntityMsg());
        }
        UserStatusEnum statusEnum = null;
        if (ObjectUtils.nullSafeEquals(user.getStatus(), UserStatusEnum.ENABLE.getValue())) {
            statusEnum = UserStatusEnum.DISABLE;
        } else if (ObjectUtils.nullSafeEquals(user.getStatus(), UserStatusEnum.DISABLE.getValue())) {
            statusEnum = UserStatusEnum.ENABLE;
        }
        if (ObjectUtils.isEmpty(statusEnum)) {
            throw new HydrauException(ExceptionEnum.DELETE, getEntityMsg());
        } else {
            user.setStatus(statusEnum.getValue());
            this.updateEntity(user, operator);
            return true;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchRemove(List<Long> ids, User operator) {
        Lists.newArrayList(ids).forEach(p -> {
            User user = super.findById(p);
            user.setStatus(UserStatusEnum.DELETE.getValue());
            this.updateEntity(user, operator);
        });
        return true;
    }
}
